#include <iostream>
#include <string>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"
using std::cerr;
using std::cin;
using std::endl;
using std::string;
using std::vector;

namespace ns_util
{
    // 便于文件读取的方法集
    class FileUtil
    {
    public:
        static bool ReadFile(const string &file_path, string &out)
        {
            // 构造函数：explicit fstream (const char* filename,
            // ios_base::openmode mode = ios_base::in | ios_base::out);
            // explicit ifstream (const string& filename, ios_base::openmode mode = ios_base::in);
            std::ifstream in(file_path, std::ios_base::in); // 输入文件流读取文件
            if (!in.is_open())
            {
                cerr << "open file " << file_path << " error" << endl;
                return false;
            }
            std::string buff;
            while (std::getline(in, buff)) // 返回的istream内部重载了operator bool
                out += buff;
            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        // 切分字符串方法
        static void CutString(string &inbuff, vector<string> &strV, string &sep)
        {
            // 选项on表示\3\3这种情况下\3之间的空串不会被提取到数组strV中
            boost::split(strV, inbuff, boost::is_any_of(sep), boost::algorithm::token_compress_on);
            return;
        }
    };

    const char *const DICT_PATH = "./dict/jieba.dict.utf8";
    const char *const HMM_PATH = "./dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char *const IDF_PATH = "./dict/idf.utf8";
    const char *const STOP_WORD_PATH = "./dict/stop_words.utf8";
    class JiebaUtil
    {
    private:
        static cppjieba::Jieba jieba;

    public:
        static void CutString(const string &src, vector<string> &out)
        {
            jieba.CutForSearch(src, out);
        }
    };
    cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
}